﻿Public Class FormCobrosListado

    Dim classController As New Class_RC_Controller.rc_Cobros()
    Dim clientes As New Class_RC_Controller.rc_Clientes

    Private m_frmCobros As FrmCobros
    Public Property frmCobros() As FrmCobros
        Get
            Return m_frmCobros
        End Get
        Set(value As FrmCobros)
            m_frmCobros = value
        End Set
    End Property

    Private Sub FormCobrosListado_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        dgCobros.AutoGenerateColumns = False
        fechaInicio.Value = DateTime.Now.AddDays(-5)

        CargarCombos()
        CargarCobros()
    End Sub

    Private Sub CargarCobros()

        Dim estado As String = ""
        If cbxEstado.Checked Then
            estado = cboEstado.Text
        End If

        Dim cobrador As String = ""
        If cbxCobrador.Checked Then
            cobrador = cboCobrador.Text
        End If

        Dim dt As New DataTable
        dt = classController.get_Cobros_All(oiml.dbConn, 0, fechaInicio.Value, fechaFin.Value, txtNro.Text, txtNombre.Text, estado, txtRuc.Text, cobrador)
        dgCobros.DataSource = dt

        If Not classController.MessageError Is Nothing Then
            MessageBox.Show("Error: " & classController.MessageError.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

    End Sub

    Private Sub CargarCombos()
        Dim dsVend As DataTable
        dsVend = clientes.get_vendedores(oiml.dbConn)
        With Me.cboCobrador
            .DataSource = dsVend
            .DisplayMember = "usuario"
            .ValueMember = "codUsuario"
        End With

        cboEstado.SelectedIndex = 0

    End Sub

    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()

    End Sub

    Private Sub btnEditar_Click(sender As System.Object, e As System.EventArgs) Handles btnEditar.Click
        Try
            If dgCobros.SelectedRows.Count > 0 Then
                Dim cod As New Integer
                cod = dgCobros.SelectedRows(0).Cells(0).Value

                If frmCobros Is Nothing Then

                    frmCobros = New FrmCobros()
                    frmCobros.codCobro = cod

                    'If frmClientes.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    frmCobros.ShowDialog()
                    CargarCobros()
                    frmCobros.Dispose()
                    frmCobros = Nothing
                    'End If
                Else
                    frmCobros.codCobro = cod
                    frmCobros.ShowDialog()
                End If

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub txtBuscar_Click(sender As System.Object, e As System.EventArgs) Handles txtBuscar.Click
        CargarCobros()
    End Sub

    Private Sub txtNombre_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtNombre.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarCobros()

        End If
    End Sub

    Private Sub dgCobros_DoubleClick(sender As System.Object, e As System.EventArgs) Handles dgCobros.DoubleClick
        btnEditar_Click(sender, e)
    End Sub

    Private Sub dgCobros_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles dgCobros.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnEditar_Click(sender, e)
        End If

    End Sub



    Private Sub txtRuc_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtRuc.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarCobros()

        End If
    End Sub

    Private Sub txtNro_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtNro.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarCobros()

        End If
    End Sub

    Private Sub cbxCobrador_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cbxCobrador.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarCobros()

        End If
    End Sub

    Private Sub cboCobrador_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cboCobrador.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarCobros()

        End If
    End Sub

    Private Sub cbxEstado_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cbxEstado.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarCobros()

        End If
    End Sub

    Private Sub cboEstado_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cboEstado.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarCobros()

        End If
    End Sub

    Private m_frmRpts As FormReportes
    Public Property frmRpts() As FormReportes
        Get
            Return m_frmRpts
        End Get
        Set(value As FormReportes)
            m_frmRpts = value
        End Set
    End Property

    Private Sub btnImprimir_Click(sender As System.Object, e As System.EventArgs) Handles btnImprimir.Click

        Dim estado As String = ""
        If cbxEstado.Checked Then
            estado = cboEstado.Text
        End If

        Dim cobrador As String = ""
        If cbxCobrador.Checked Then
            cobrador = cboCobrador.Text
        End If

        Dim dt As New DataTable
        dt = classController.get_Cobros_Facturas_All(oiml.dbConn, 0, fechaInicio.Value, fechaFin.Value, txtNro.Text, txtNombre.Text, estado, txtRuc.Text, cobrador)

        If frmRpts Is Nothing Then
            frmRpts = New FormReportes()
            'frmRpts.MdiParent = Me
            frmRpts.tipo_Reporte = "COBROS"
            frmRpts.datos_Reporte = dt
            frmRpts.ShowDialog()
        Else

            If Not frmRpts.Visible Then
                frmRpts = New FormReportes()
                'frmRpts.MdiParent = Me
                frmRpts.tipo_Reporte = "COBROS"
                frmRpts.datos_Reporte = dt
                frmRpts.ShowDialog()
            Else
                frmRpts.tipo_Reporte = "COBROS"
                frmRpts.datos_Reporte = dt
                frmRpts.Select()
                frmRpts.Activate()
            End If

        End If

        If Not classController.MessageError Is Nothing Then
            MessageBox.Show("Error: " & classController.MessageError.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

    End Sub

End Class